package wx.controls
{

	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import wx.core.ITheme;
	import wx.core.SettingCache;


	/**
	 * 
	 * @author wujianqi
	 * 单选控件
	 * 
	 */	
	public class Radio extends ButtonBase
	{
		
		private var _selected:Boolean = false;
		private var _text:String;
		private var _label:Label;
		
		private var _group:RadioGroup;
		private var _items:Vector.<Radio>;
		
		public var value:Object;
		
		private const _theme:ITheme = SettingCache.theme;
		
		public function Radio($label:String,$group:RadioGroup=null)
		{

			var up:DisplayObject =   _theme.radioNormalSkin;
			var hover:DisplayObject = _theme.radioHoverSkin;
			var down:DisplayObject = _theme.radioDownSkin;
			var disabled:DisplayObject =  _theme.radioDisableSkin;
			super(up,hover,down,disabled);

			_text = $label;
			addLabel();
			
			_group = $group;
			addItem();
			
			if(_enable) bindEvent();
		}
		
		private function addItem():void{
			_items = _group ?_group.items :null;
			if(_items!=null && _items.indexOf(this)==-1) _items.push(this);
		}		
		private function removeItem():void{
			if(_group){
				if(_items.length==0){
					_items = null;
					_group = null;
				}else _items.splice(_items.indexOf(this),1);
			}
		}
		
		
		public function set group(radioGroup:RadioGroup):void
		{
			removeItem();
			_group = radioGroup;
			addItem();
		}		
		

		private function onMouseClick(e:MouseEvent):void{
			if(_group!=null) _group.selectValue = value?value:_text;
		}
		private function bindEvent():void{
			addEventListener(MouseEvent.CLICK,onMouseClick);
		}
		
		private function unbindEvent():void{
			removeEventListener(MouseEvent.CLICK,onMouseClick);
		}

		public function get label():String
		{
			return _text;
		}

		public function set label(text:String):void
		{
			if(_text != text){
				_text = text;
				updateLabelDisplay();
			}
		}

		public function get selected():Boolean
		{
			return _selected;
		}

		public function set selected(state:Boolean):void
		{
			if(_selected != state){
				_selected = state;
				super.upSkin = _selected?_theme.radioSelectedSkin:_theme.radioNormalSkin;
				super.hoverSkin =  _selected?_theme.radioSelectedHoverSkin:_theme.radioHoverSkin;
				super.updateState();
			}
		}
		
		
		private  function updateLabelDisplay():void
		{
			if(contains(_label)){
				removeChild(_label);
				addLabel();
			}			
		}

		private function addLabel():void{
			var w:Number = this.width;
			var h:Number = this.height;
			_label = new Label(_text);
			_label.x = w+2;
			addChild(_label);
			if(_label.height>h) _label.y = (_label.height - h)/2;
			else _label.y = (h-_label.height)/2
		}
		
		override public function set enable(value:Boolean):void{
			if(_enable != value){
				super.enable = value;
				if(!_enable) unbindEvent();
				else bindEvent();
			}			
		}
		
		override protected function dispose():void{
			super.dispose();
			unbindEvent();
			removeItem();
		}

	}
}